﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zichan.BLL;
using zichan.Entity;
using zichan.Models;

namespace zichan.Controllers
{
    public class ServerPurposeSubClassController : Controller
    {
        // GET: ServerPurposeSubClass
        public ActionResult Index(string name, int? page, int? pageSize, int? pageCount, int? selectedPage,int purposeId)
        {
            page = page ?? 1;
            pageSize = pageSize ?? 10;
            if (selectedPage.HasValue)
                page = selectedPage;
            if (pageCount.HasValue)
                page = page <= pageCount ? page : pageCount;
            if (page < 1) page = 1;
            ViewBag.page = page;
            ViewBag.pageSize = pageSize;
            ViewBag.selectedPage = selectedPage;
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "10", Text = "10" }, new SelectListItem { Value = "20", Text = "20" }, new SelectListItem { Value = "50", Text = "50" }, new SelectListItem { Value = "100", Text = "100" } };
            ViewBag.pageSizes = new SelectList(items, "Value", "Text", ViewBag.pageIndex);

            ViewBag.name = name;
            ViewBag.purposeId = purposeId;
            return View(ServerPurposeSubClassService.ServerPurposeSubClassList(name,purposeId ,pageSize.Value, page.Value));
        }
        public ActionResult Create(int purposeId)
        {
            ViewBag.maxSort = ServerPurposeSubClassService.MaxSort() + 1;
            ViewBag.purposeId = purposeId;
            return View();
        }
        [HttpPost]
        public ActionResult Create(ServerPurposeSubClass model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.maxSort = ServerPurposeSubClassService.MaxSort() + 1;
                return View(model);
            }
            string err = ServerPurposeSubClassService.ServerPurposeAdd(model);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#添加成功";
                //ClassLogService.AddLog("服务器用途分类", "添加" + "\"" + model.serverPurposeSubName + "\"");
            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("Index",new { purposeId=model.serverPurposeId});
        }
        public ActionResult CreateList(int purposeId)
        {
            ViewBag.purposeId = purposeId;
            return View();
        }
        [HttpPost]
        public ActionResult CreateList(ListModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            List<string> list = model.nameList.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            List<string> existName;
            List<string> nameListForLog;
            string err = ServerPurposeSubClassService.AddBatchClass(list, out existName, out nameListForLog,model.purposeId);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#添加成功";
                if (existName.Count() > 0)
                {
                    TempData["alert"] = "error#" + string.Join("、", existName) + "因重命名无法添加";
                }
                //if (nameListForLog.Count() > 0)
                //{
                //    ClassLogService.AddLog("服务器用途分类", "添加" + "\"" + string.Join("、", nameListForLog) + "\"");
                //}
            }
            else
                TempData["alert"] = "error#添加失败";

            return RedirectToAction("Index", new { purposeId = model.purposeId });
        }
        public ActionResult Edit(int id)
        {
            ServerPurposeSubClass cabinetClass = ServerPurposeSubClassService.Get(id);
            if (cabinetClass == null)
                return HttpNotFound();

            return View(cabinetClass);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ServerPurposeSubClass model)
        {
            if (!ModelState.IsValid)
                return View(model);
            string oldName = ServerPurposeSubClassService.Get(model.serverPurposeSubId).serverPurposeSubName;
            string err = ServerPurposeSubClassService.ServerPurposeEdit(model);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#修改成功";
                ClassLogService.AddLog("服务器用途分类", "修改" + "\"" + oldName + "\"为\"" + model.serverPurposeSubName + "\"");
            }
            else
                TempData["alert"] = "error#" + err;
            return RedirectToAction("index", new { purposeId = model.serverPurposeId });
        }

        public ActionResult Delete(int id,int purposeId)
        {
            if (id == 1||id==2)
            {
                TempData["alert"] = "error#无法删除固定的类型";
                return RedirectToAction("index", new { purposeId = purposeId });
            }
            string err = ServerPurposeSubClassService.ServerPurposeDelete(id);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#删除成功";
            }
            else
                TempData["alert"] = "error#" + err;

            return RedirectToAction("index", new { purposeId = purposeId });
        }
    }
}